/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package echoserverssl;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
import javax.security.cert.CertificateException;

/**
 *
 * @author Maciek
 */
public class EchoClientSSL {

    /**
     * @param args the command line arguments
     */
    private static SSLSocketFactory getFactory(File pKeyFile, String pKeyPassword) throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, KeyManagementException, java.security.cert.CertificateException{
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
            KeyStore trustStore = KeyStore.getInstance("JKS");
            SSLContext ctx;
            ctx = SSLContext.getInstance("TLS");
            
            trustStore.load(new FileInputStream(pKeyFile), pKeyPassword.toCharArray());
            trustManagerFactory.init(trustStore);
            ctx.init(null,trustManagerFactory.getTrustManagers(), new SecureRandom());
            return ctx.getSocketFactory();
    }
    
    public static void main(String[] args) {
        try {
            // SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocketFactory sslsocketfactory = (SSLSocketFactory) getFactory(new File("clientkeystore.jks"), "bskbsk");
            SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 9999);
            sslsocket.setUseClientMode(true);
            InputStream inputstream = System.in;
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

            OutputStream outputstream = sslsocket.getOutputStream();
            OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
            BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);

            String string = null;
            while ((string = bufferedreader.readLine()) != null) {
                bufferedwriter.write("client" + string + '\n');
                bufferedwriter.flush();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    
    }
}
